AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

ENT.ID = ""
ENT.Position = Vector(0,0,0)
ENT.Target	= nil
ENT.FireDelay = 0
ENT.NextAttack = CurTime() + ENT.FireDelay		
ENT.Reference = nil
ENT.TType = "atg"
ENT.Level = 1
ENT.TurretOwner = nil

function ENT:Initialize()
	
	self:SetModel( "models/Combine_turrets/Floor_turret.mdl" )
	
	local allTurrets = ents.FindByClass("adv_turret_gun")
	self.ID = self.TType.."_"..tostring(#allTurrets)
	
	-- initial turret set up
		self.Reference = TURRETS.AllTurrets[self.TType..self.Level]
		self.Damage = self.Reference.Damage
		self.Range = self.Reference.Range
		self.FireDelay = self.Reference.FireDelay
		
	self:PhysicsInit( SOLID_VPHYSICS )
	self.Position = self:GetPos()
end


function ENT:Owner(pl)
	self.TurretOwner = pl
end


function ENT:AttackTarget(ang)
	if CurTime() >= self.NextAttack then
		local bullet = {}
		bullet.Num=1
		bullet.Src=self:GetAttachment(self:LookupAttachment("light")).Pos
		bullet.Dir=self:GetAngles():Forward() -- always hits targets
		--bullet.Dir=ang:Forward()
		bullet.Spread=Vector(0,0,0)
		bullet.Tracer=1	
		bullet.Force=1
		bullet.Attacker = self.TurretOwner
		bullet.Damage=self.Damage
		bullet.Callback	= function ( attacker, tr, dmginfo ) 
							--print(dmginfo:GetDamage())
                         end	
						 
		self:FireBullets(bullet)
		self.NextAttack = CurTime() + self.FireDelay	 
	end
end

function ENT:RotateToTarget()
	timer.Create("AimAtTargetROCKET"..self:EntIndex(), 0.1, 0, function()
		
		if self:GetPos():Distance(self.Target:GetPos()) > self.Range or not self.Target:Alive() then 
			timer.Destroy("AimAtTargetROCKET"..self:EntIndex())
			self.Target = nil
			self.AttackCounter = 0
			return
		end
		
		local targetPos = self.Target:GetPos()
		local ang = (targetPos - self.Position):Angle()		
		
		ang.r, ang.p, ang.y = 0, math.NormalizeAngle(ang.p), math.NormalizeAngle(ang.y)		
		self:SetAngles(ang)

		ang = targetPos:Angle()		
		self:AttackTarget(ang)
	end)
end


function ENT:Think()	
	self:NextThink( CurTime() + 1 )	
	
	if not self.Target then
		for _, pl in pairs(player.GetAll()) do
			if self.Position:Distance(pl:GetPos()) < self.Range then --and self.TurretOwner != pl then
				self.Target = pl
				umsg.Start("targetRocket")
					umsg.Entity(self.Target)
				umsg.End()
				self:RotateToTarget()
			end
		end		
		self:NextThink( CurTime() + 0.1 )
	end				
	
    return true
end

function ENT:OpenMenu()
	umsg.Start("OpenTurretInfo")
		umsg.String(self.ID)
		umsg.Short(self.Level)
	umsg.End()
end

function ENT:Use(activator, caller)
	if caller:IsPlayer() then
		self:OpenMenu()
	end
end


function ENT:Upgrade()
	local newLevel = self.Level + 1
	if not TURRETS.AllTurrets[self.TType..newLevel] then return false end
	
	self.Level = newLevel
	self.Reference = TURRETS.AllTurrets[self.TType..self.Level]
	self.Damage = self.Reference.Damage
	self.Range = self.Reference.Range
	self.FireDelay = self.Reference.FireDelay
	self:OpenMenu()
	return true
end